package com.dataextend.controller;

import com.dataextend.entity.*;
import com.dataextend.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(value = "角色权限表接口")
@RestController
@RequestMapping("/api/role/permission")
@CrossOrigin(origins={"http://localhost:8080"},allowCredentials="true")
public class RootRolePermissionController extends RootBaseApiController {

    @Autowired
    private RolePermissionService rolePermissionService;

    @ApiOperation("添加角色权限")
//    @RequiresPermissions("role:permission:add")
    @PostMapping("/add")
    public Map<String, Object> add(@RequestBody RolePermissionAddRequest rolePermissionAddRequest){
        Long roleId = rolePermissionAddRequest.getRoleId();
        for (Long permissionId : rolePermissionAddRequest.getPermissionIds()){
            RolePermissionEntity rolePermissionEntity = new RolePermissionEntity()
                    .setRoleId(roleId)
                    .setPermissionId(permissionId);
            rolePermissionService.add(rolePermissionEntity);
        }
//        if (rolePermissionService.add(rolePermissionEntity)==0) {
//            return resp(406,"添加失败");
//        } else {
//            return resp(200, "添加成功", rolePermissionEntity);
//        }

        return resp(200, "添加成功", "success");
    }

    @ApiOperation("修改角色权限信息")
//    @RequiresPermissions("role:permission:update")
    @PostMapping("/update")
    public Map<String, Object> update(@RequestBody RolePermissionAddRequest rolePermissionAddRequest) {
//        RolePermissionEntity rolePermissionEntity = new RolePermissionEntity()
//                .setId(id)
//                .setRoleId(roleId)
//                .setPermissionId(permissionId);
//        rolePermissionService.update(rolePermissionEntity)

        long roleId = rolePermissionAddRequest.getRoleId();
        rolePermissionService.deleteRoleId(roleId);
        for (Long permissionId : rolePermissionAddRequest.getPermissionIds()){
            rolePermissionService.addRoleIdPermission(roleId, permissionId);
        }

        return resp(200, "修改成功", "success");
    }

    @ApiOperation("删除角色权限")
//    @RequiresPermissions("role:permission:delete")
    @PostMapping("/delete")
    public Map<String, Object> deleteById(@RequestBody RolePermissionDeleRequest rolePermissionDeleRequest) {
        for (long id : rolePermissionDeleRequest.getIds()){
            rolePermissionService.delete(id);
        }
        return resp(200, "删除成功");
    }

    @ApiOperation("根据id查询角色权限信息")
    @GetMapping("/query")
    public Map<String, Object> queryById(@RequestParam(value = "id", required = false) Long id) {
        return resp(200, "查询成功", rolePermissionService.queryById(id));
    }

    @ApiOperation("查询所有角色权限信息")
    @GetMapping("/list")
    public Map<String, Object> list(@RequestParam(value = "id", required = false) Long id,
                                    @RequestParam(value = "roleId", required = false) Long roleId,
                                    @RequestParam(value = "permissionId", required = false) Long permissionId) {
        RolePermissionEntity rolePermissionEntity = new RolePermissionEntity()
                .setId(id)
                .setRoleId(roleId)
                .setPermissionId(permissionId);

        return resp(200, "查询所有成功", rolePermissionService.list(rolePermissionEntity));
    }
}
